package com.jeeplus.test.jlnu.archivalInfo.controller;

import com.alibaba.excel.EasyExcel;
import com.jeeplus.test.jlnu.admissionInfo.utils.MyWebSocketHandler;
import com.jeeplus.test.jlnu.archivalInfo.domain.vo.ArchivalInfoExcelVO;
import com.jeeplus.test.jlnu.archivalInfo.domain.vo.ArchivalInfoReqVo;
import com.jeeplus.test.jlnu.archivalInfo.service.ExportService;
import com.jeeplus.test.jlnu.archivalInfo.utils.ArchivalInfoDataListener;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;

@Api(tags = "档案信息导出")
@RestController
@Slf4j
@RequestMapping("/exportArchives")
public class ExportExcelController {
    @Autowired
    private MyWebSocketHandler webSocketHandler;
    @Autowired
    private ExportService exportService;
    /**
     * 档案信息导出
     * @return
     * @throws IOException
     */
    @ApiOperation("档案信息导出")
    @GetMapping("/exportArchivesFiles")
        public void importArchives( ArchivalInfoReqVo archivalInfoExcelVO, @RequestParam(required = true)String officeId, HttpServletResponse response) throws IOException {
        // 1. 生成 ZIP 文件路径
        String zipFilePath = System.getProperty("java.io.tmpdir") + "output.zip";

        if(archivalInfoExcelVO.getApproveStateList()!=null && archivalInfoExcelVO.getApproveStateList().size()!=0){
            exportService.exportService(archivalInfoExcelVO, archivalInfoExcelVO.getStudentIds(), officeId);
        }else{
            archivalInfoExcelVO.setApproveStateList(new ArrayList<String>(){{
                add("3");
            }});
            exportService.exportService(archivalInfoExcelVO, archivalInfoExcelVO.getStudentIds(), officeId);
        }
        // 2. 调用 `exportService` 生成 ZIP 文件


        // 3. **手动将 ZIP 文件写入 response**
        File zipFile = new File(zipFilePath);
        if (!zipFile.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "文件未找到");
            return;
        }

        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"批量下载.zip\"");
        response.setContentLength((int) zipFile.length());

        // 4. **流式拷贝 ZIP 文件到 response**
        try (FileInputStream fis = new FileInputStream(zipFile);
             ServletOutputStream os = response.getOutputStream()) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5. **可选：删除临时 ZIP 文件**
            zipFile.delete();
        }
    }
    @ApiOperation("信息导出")
    @GetMapping("/onlyeExportArchivesFiles")
    public void onlyExportArchives( ArchivalInfoReqVo archivalInfoExcelVO, @RequestParam(required = true)String officeId, HttpServletResponse response) throws IOException {
        // 1. 生成 ZIP 文件路径
        String zipFilePath = System.getProperty("java.io.tmpdir") + "output.zip";

        if(archivalInfoExcelVO.getApproveStateList()!=null && archivalInfoExcelVO.getApproveStateList().size()!=0){
            exportService.onlyExportService(archivalInfoExcelVO, archivalInfoExcelVO.getStudentIds(), officeId);
        }else{
            archivalInfoExcelVO.setApproveStateList(new ArrayList<String>(){{
                add("3");
            }});
            exportService.onlyExportService(archivalInfoExcelVO, archivalInfoExcelVO.getStudentIds(), officeId);
        }
        // 2. 调用 `exportService` 生成 ZIP 文件


        // 3. **手动将 ZIP 文件写入 response**
        File zipFile = new File(zipFilePath);
        if (!zipFile.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "文件未找到");
            return;
        }

        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"批量下载.zip\"");
        response.setContentLength((int) zipFile.length());

        // 4. **流式拷贝 ZIP 文件到 response**
        try (FileInputStream fis = new FileInputStream(zipFile);
             ServletOutputStream os = response.getOutputStream()) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5. **可选：删除临时 ZIP 文件**
            zipFile.delete();
        }
    }
}
